package com.caigou.modules.service.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.service.baen.PlatformItemBean
import com.caigou.modules.service.baen.WebsiteCollectBean
import com.caigou.modules.service.repository.ServiceRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/11/11
 */
@HiltViewModel
class ServiceViewModel @Inject constructor(private val repository: ServiceRepository) : ViewModel() {

    val websiteCollectListLiveData = MutableLiveData<Resource<WebsiteCollectBean>>()
    val websiteCollectDeleteLiveData = MutableLiveData<Resource<String>>()
    val websiteCollectAddLiveData = MutableLiveData<Resource<String>>()
    val platformListLiveData = MutableLiveData<Resource<MutableList<PlatformItemBean>>>()

    fun getWebsiteCollectList(page: Long, limit: Long, userId: String) {
        websiteCollectListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getWebsiteCollectList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<WebsiteCollectBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    websiteCollectListLiveData.value = it
                }
        }
    }

    fun deleteWebsiteCollect(userId: String, websiteId: Int) {
        websiteCollectDeleteLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.deleteWebsiteCollect(userId, websiteId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    websiteCollectDeleteLiveData.value = it
                }
        }
    }

    fun addWebsiteCollect(userId: String, nickName: String, url: String) {
        websiteCollectDeleteLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.addWebsiteCollect(userId, nickName, url)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    websiteCollectDeleteLiveData.value = it
                }
        }
    }

    fun getPlatformList() {
        platformListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPlatformList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PlatformItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    platformListLiveData.value = it
                }
        }
    }
}